<template>
  <el-tooltip :content="toolTip" placement="top">
    <svg-icon icon-class="五角星" class="subscribe-icon" :class-name="subscribeFlag?'subscribed-style':'not-subscribed-style'" @click="subscribeHandler" />
  </el-tooltip>
</template>

<script>
import { mapGetters } from 'vuex'
import { add, del } from '@/api/front/userSubscribe/userSubscribe'

export default {
  name: 'Test',
  props: {
    codeNumber: {
      type: String,
      default: ''
    },
    codeName: {
      type: String,
      default: ''
    }
  },

  data() {
    return {
      subscribeFlag: false
    }
  },

  computed: {
    ...mapGetters([
      'user',
      'subscribeFunds'
    ]),

    toolTip() {
      return this.subscribeFlag ? '取消订阅' : '订阅基金'
    },

    subscribeFundInfo() {
      if (this.subscribeFunds.content) {
        const arr = this.subscribeFunds.content
        const newArr = arr.filter(item => item.codeNumber === this.codeNumber)
        return newArr
      }
      return []
    }
  },

  watch: {
    subscribeFundInfo(val) {
      console.log(val)
      this.subscribeFlag = val.length > 0
    }
  },

  created() {
    this.fitchSubscribeFunds()
  },

  methods: {

    // 查询自己的基金列表
    fitchSubscribeFunds() {
      if (this.user.id) {
        this.$store.dispatch('QuerySubscribeFunds', this.user.id)
      }
    },

    // 订阅点击处理
    subscribeHandler() {
      // 用户是否登录，用户登录允许订阅
      if (this.user.id) {
        if (this.subscribeFlag) {
          // 用户已经订阅，则调用删除订阅
          const ids = this.subscribeFundInfo.map(item => (item.id))
          del(ids).then(res => {
            this.$message.success('取消订阅成功！')
            // 取消订阅后刷新用户订阅信息
            this.$store.dispatch('QuerySubscribeFunds', this.user.id)
          })
        } else {
          // 新增基金订阅表单数据
          const postData = {
            codeName: this.codeName,
            codeNumber: this.codeNumber,
            fallReminder: -3,
            increaseReminder: 3,
            userId: this.user.id
          }
          // 用户没有订阅，则调用新增订阅
          add(postData).then(res => {
            switch (res) {
              case -1:
                this.$message.error('基金订阅失败，请稍后重试！')
                break
              case 0:
                this.$message.info('基金订阅数量达到上限！')
                break
              case 1:
                this.$message.success('基金订阅成功！')
                break
            }
            // 新增订阅后刷新用户订阅信息
            this.$store.dispatch('QuerySubscribeFunds', this.user.id)
          })
        }
      } else {
        // 用户没有登录，不允许订阅，提示用户是否去订阅
        this.$confirm('登录后才可以订阅哦, 是否登录?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 用户确认登录
          this.$router.push(`/login?redirect=${this.$route.fullPath}`)
        }).catch(() => {
          // 用户取消登录
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>

.subscribe-icon{
  cursor: pointer;

}

.subscribed-style{
  fill: #EA9518 !important;
}

.not-subscribed-style{
  fill: #dddddd !important;
}

.shaky {
  display: inline-block;
  padding: 1px;
  //font-size: 12px;

  -webkit-transform-origin: center center;
  -ms-transform-origin: center center;
  transform-origin: center center;
  -webkit-animation-name: shaky-slow;
  -ms-animation-name: shaky-slow;
  animation-name: shaky-slow;
  -webkit-animation-duration: 4s;
  -ms-animation-duration: 4s;
  animation-duration: 4s;
  -webkit-animation-iteration-count: infinite;
  -ms-animation-iteration-count: infinite;
  animation-iteration-count: infinite;
  -webkit-animation-timing-function: ease-in-out;
  -ms-animation-timing-function: ease-in-out;
  animation-timing-function: ease-in-out;
  -webkit-animation-delay: 0s;
  -ms-animation-delay: 0s;
  animation-delay: 0s;
  -webkit-animation-play-state: running;
  -ms-animation-play-state: running;
  animation-play-state: running;
}

@keyframes shaky-slow {
  0% {
    transform: translate(0px, 0px) rotate(0deg);
  }
  2% {
    transform: translate(-1px, 1.5px) rotate(1.5deg);
  }
  4% {
    transform: translate(1.3px, 0px) rotate(-0.5deg);
  }
  6% {
    transform: translate(1.4px, 1.4px) rotate(-2deg);
  }
  8% {
    transform: translate(-1.3px, -1px) rotate(-1.5deg);
  }
  10% {
    transform: translate(1.4px, 0px) rotate(-2deg);
  }
  12% {
    transform: translate(-1.3px, -1px) rotate(-2deg);
  }
  14% {
    transform: translate(1.5px, 1.3px) rotate(1.5deg);
  }
  16% {
    transform: translate(1.5px, -1.5px) rotate(-1.5deg);
  }
  18% {
    transform: translate(1.3px, -1.3px) rotate(-2deg);
  }
  20% {
    transform: translate(1px, 1px) rotate(-0.5deg);
  }
  22% {
    transform: translate(1.3px, 1.5px) rotate(-2deg);
  }
  24% {
    transform: translate(-1.4px, -1px) rotate(2deg);
  }
  26% {
    transform: translate(1.3px, -1.3px) rotate(0.5deg);
  }
  28% {
    transform: translate(1.6px, -1.6px) rotate(-1.5deg);
  }
  30% {
    transform: translate(-1.3px, -1.3px) rotate(-1.5deg);
  }
  32% {
    transform: translate(-1px, 0px) rotate(2deg);
  }
  34% {
    transform: translate(1.3px, 1.3px) rotate(-0.5deg);
  }
  36% {
    transform: translate(1.3px, 1.6px) rotate(1.5deg);
  }
  38% {
    transform: translate(1.3px, -1.6px) rotate(1.5deg);
  }
  40% {
    transform: translate(-1.4px, -1px) rotate(-0.5deg);
  }
  42% {
    transform: translate(-1.4px, 1.3px) rotate(-0.5deg);
  }
  44% {
    transform: translate(-1.6px, 1.4px) rotate(0.5deg);
  }
  46% {
    transform: translate(-2.1px, -1.3px) rotate(-0.5deg);
  }
  48% {
    transform: translate(1px, 1.6px) rotate(1.5deg);
  }
  50% {
    transform: translate(1.6px, 1.6px) rotate(1.5deg);
  }
  52% {
    transform: translate(-1.4px, 1.6px) rotate(0.5deg);
  }
  54% {
    transform: translate(1.6px, -1px) rotate(-2deg);
  }
  56% {
    transform: translate(1.3px, -1.6px) rotate(-2deg);
  }
  58% {
    transform: translate(-1.3px, -1.6px) rotate(0.5deg);
  }
  60% {
    transform: translate(1.3px, 1.6px) rotate(-0.5deg);
  }
  62% {
    transform: translate(0px, 0px) rotate(-1.5deg);
  }
  64% {
    transform: translate(-1.6px, -1.6px) rotate(-2deg);
  }
  66% {
    transform: translate(1.6px, -1.6px) rotate(0.5deg);
  }
  68% {
    transform: translate(0px, -1.6px) rotate(-2deg);
  }
  70% {
    transform: translate(-1.6px, 1px) rotate(1.5deg);
  }
  72% {
    transform: translate(-1.6px, 1.6px) rotate(2deg);
  }
  74% {
    transform: translate(1.3px, -1.6px) rotate(-0.5deg);
  }
  76% {
    transform: translate(1.4px, 1px) rotate(-0.5deg);
  }
  78% {
    transform: translate(-1px, 1.4px) rotate(2deg);
  }
  80% {
    transform: translate(1.4px, 1.6px) rotate(2deg);
  }
  82% {
    transform: translate(-1.6px, -1.6px) rotate(-0.5deg);
  }
  84% {
    transform: translate(-1.4px, 1.4px) rotate(-2deg);
  }
  86% {
    transform: translate(1px, 1.4px) rotate(-2deg);
  }
  88% {
    transform: translate(-1.4px, 1.4px) rotate(-1.5deg);
  }
  90% {
    transform: translate(-1.6px, -1.6px) rotate(-2deg);
  }
  92% {
    transform: translate(-1.4px, 1.6px) rotate(2deg);
  }
  94% {
    transform: translate(-1.6px, -1.6px) rotate(-2deg);
  }
  96% {
    transform: translate(-1.4px, 1.3px) rotate(-2deg);
  }
  98% {
    transform: translate(1.3px, 1px) rotate(-0.5deg);
  }
}
</style>
